﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="categoryTable" lay-filter="categoryTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="categoryTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#categoryTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="categoryTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除{{d.nickname}}吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="categoryTbState">
    <input type="checkbox" lay-filter="categoryTbStateCk" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.status==1?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="categoryEditDialog">
    <form id="categoryEditForm" lay-filter="categoryEditForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">分类上级:</label>
            <div class="layui-input-block">
                <select name="pid" lay-verType="tips" lay-verify="required" required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">分类名称:</label>
            <div class="layui-input-block">
                <select name="cate_name" lay-verType="tips" lay-verify="required" required></select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="categoryEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'treeTable', 'admin', 'formX', 'dropdown', 'setter', 'notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var treeTable = layui.treeTable;
        var admin = layui.admin;
        var formX = layui.formX;
        var setter = layui.setter;
        var notice = layui.notice;
        var categoryPList = [];
        form.render('select', 'categoryTbSearchForm');

        /* 渲染表格 */
        var insTb = treeTable.render({
            elem: '#categoryTable',
            text: {
                none: '暂无相关数据'
            },
            tree: {
                iconIndex: 1,
                idName: 'id',
                pidName: 'pid',
                isPidData: true,
                getIcon: ''
            },
            height: 'full-170',
            defaultToolbar: '',
            cols: [[
                {field: 'id', title: 'id',width: 60},
                {field: 'cate_name', title: '分类名'},
                // {field: 'status', title: '状态', templet: '#categoryTbState', width: 100},
                {title: '操作', toolbar: '#categoryTbBar', align: 'center', width: 140,minWidth: 140, fixed: 'right'}
            ]],
            reqData: function (data, callback, where) {
                admin.req(setter.baseServer + '/content_cate/cate_list', function (res) {
                    var tbDataList = res.data ? res.data : [];

                    for (var i = 0; i < tbDataList.length; i++) {
                        tbDataList[i].open = true;
                        if(tbDataList[i].pid==0){
                            categoryPList.push(tbDataList[i])
                        }
                    }
                    callback(tbDataList);
                });
            }
        });

        /* 表格搜索 */
        form.on('submit(categoryTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(categoryTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(categoryTable)', function (obj) {
            if ('add' === obj.event) { // 添加
                showEditModel();
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '分类',
                content: $('#categoryEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('categoryEditForm', mData);
                    // 表单提交事件
                    form.on('submit(categoryEditSubmit)', function (data) {
                        if(mData){
                            data.field.id=mData.id;
                        }
                        admin.req((mData ? '/content_cate/cate_edit' : '/content_cate/cate_add'), JSON.stringify(data.field), function (res) {
                            if (200 === res.code) {
                                layer.close(dIndex);
                                notice.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                notice.msg(res.msg, {icon: 2, anim: 6});
                            }
                        }, 'post');
                        return false;
                    });
                    // 渲染角色下拉
                    formX.renderSelect({
                        elem: '[lay-filter="categoryEditForm"] select[name="rid"]',
                        data: categoryPList,
                        name: 'role_name',
                        value: 'id',
                        hint: '请选择角色',
                        initValue: mData ? mData.rid : undefined
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }
    });
</script>
